﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using Aliphant.UI.Models;

using Microsoft.Web.WebView2.Core;

namespace TestModule.Widgets
{
    /// <summary>
    /// Demo.xaml 的交互逻辑
    /// </summary>
    public partial class Demo : Window
    {
        private double ScreenWidth = 0;
        private double ScreenHeight = 0;

        //private bool isLoad = false;

        public Demo()
        {
            InitializeComponent();
            InitializeWebView2();
            ScreenWidth = SystemParameters.VirtualScreenWidth;
            ScreenHeight = SystemParameters.VirtualScreenHeight;
            webView.Source = new Uri("https://www.bing.com");
        }

        private async void InitializeWebView2()
        {
            var userDataFolder = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "XDeskTool/WebView2Data");

            var env = await CoreWebView2Environment.CreateAsync(
                browserExecutableFolder: null,
                userDataFolder: userDataFolder
            );
            // 关键：必须在设置 Source 前初始化环境
            await webView.EnsureCoreWebView2Async(env);
        }

        /// <summary>
        /// 拖动窗口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            try
            {
                DragMove();

                if (Right <= 0)
                {
                    this.Left = ScreenWidth - 20;
                    _location = WindowLocationStyle.Hide;
                }
                else if (Left <= 0)
                {
                    this.Right = ScreenWidth - 20;
                    _location = WindowLocationStyle.Hide;
                }
                else if (Top <= 0)
                {
                    this.Bottom = ScreenHeight - 20;
                    _location = WindowLocationStyle.Hide;
                }
                else if (Bottom <= 0)
                {
                    this.Top = ScreenHeight - 20;
                    _location = WindowLocationStyle.Hide;
                }
                else
                {
                    _location = WindowLocationStyle.Normal;
                }
            }
            catch { }
        }

        private WindowLocationStyle _location = WindowLocationStyle.Normal;
        private bool _isInWindow = false;
        private void DockPanel_MouseEnter(object sender, MouseEventArgs e)
        {
            try
            {
                if (_location == WindowLocationStyle.Hide)
                {
                    if (this.Left == ScreenWidth - 20)
                    {
                        this.Right = 0;
                    }
                    else if (this.Right == ScreenWidth - 20)
                    {
                        this.Left = 0;
                    }
                    else if (this.Bottom == ScreenHeight - 20)
                    {
                        this.Top = 0;
                    }
                    else if (this.Top == ScreenHeight - 20)
                    {
                        this.Bottom = 0;
                    }
                    _isInWindow = true;
                    _location = WindowLocationStyle.Over;
                }
            }
            catch { }
        }

        private void DockPanel_MouseLeave(object sender, MouseEventArgs e)
        {
            try
            {
                DoGetCursorPos(out Aliphant.Datas.Point point);
                if (point.X >= Left + 20 && point.X <= (Left + body.Width + 20) && point.Y >= Top + 20 && point.Y <= (Top + body.Height + 20))
                {
                    _isInWindow = true;
                }
                else
                {
                    _isInWindow = false;
                }
                if (_location == WindowLocationStyle.Over && !_isInWindow)
                {
                    if (this.Right == 0)
                    {
                        this.Left = ScreenWidth - 20;
                    }
                    else if (this.Left == 0)
                    {
                        this.Right = ScreenWidth - 20;
                    }
                    else if (this.Top == 0)
                    {
                        this.Bottom = ScreenHeight - 20;
                    }
                    else if (this.Bottom <= 0)
                    {
                        this.Top = ScreenHeight - 20;
                    }
                    _location = WindowLocationStyle.Hide;
                }
            }
            catch { }
        }


        [DllImport("user32.dll")]
        public static extern IntPtr GetCursorPos(out Aliphant.Datas.Point lpPoint);

        private void DoGetCursorPos(out Aliphant.Datas.Point point)
        {
            IntPtr result = GetCursorPos(out Aliphant.Datas.Point tagPOINT);
            if (result.ToInt32() == 1)
            {
                point = tagPOINT;
            }
            else
            {
                point = new Aliphant.Datas.Point();
            }
        }

        public double Right
        {
            get => ScreenWidth - this.Left - body.Width - 40;
            set
            {
                this.Left = ScreenWidth - value - body.Width - 40;
            }
        }

        public double Bottom
        {
            get => ScreenHeight - this.Top - body.Height - 40;
            set
            {
                this.Top = ScreenHeight - value - body.Height - 40;
            }
        }

        private void Window_KeyUp(object sender, KeyEventArgs e)
        {

        }

        private void Window_KeyDown(object sender, KeyEventArgs e)
        {

        }
    }
}
